var e = false;
Page({
  data: {
    page: 0,
    page_count: 0,
    row_count: 0,
    shop_list: [],
    longitude: "",
    latitude: "",
    keyword: "",
    myAddress: '',
    current_shop: null,
    current_city: null,
    titleShow: true
  },
  onLoad: function (t) {
    var e = this;
    getApp().page.onLoad(this, t);
    var city = wx.getStorageSync('store_info');
    var shop = getApp().core.getStorageSync(getApp().const.ADDRESS_INFO);
    this.setData({
      current_city: city ? city : this.data.store,
      current_shop: shop
    });
    getApp().core.getLocation({
      success: function (t) {
        e.setData({
          longitude: t.longitude,
          latitude: t.latitude
        });
      },
      complete: function () {
        e.locationContent();
        e.loadData();
      }
    });
  },
  onPageScroll: function (e) {
    if (this.data.store.client_shop_select_module != 'shop') {
      return;
    }
    if (e.scrollTop <= 200) {
      setTimeout(() => {
        this.setData({
          titleShow: true
        })
      }, 50)
    } else {
      setTimeout(() => {
        this.setData({
          titleShow: false
        })
      }, 50)
    }
  },
  onInput: function (t) {
    this.setData({
      keyword: t.detail.value
    });
  },
  onInputConfirm: function () {
    this.setData({
      page: 0
    })
    this.loadData();
  },
  onDeleteInput: function () {
    this.setData({
      page: 0,
      keyword: ''
    });
    this.loadData();
  },
  onCallMobile: function(e) {
    var mobile = e.currentTarget.dataset.mobile;
    getApp().core.makePhoneCall({
        phoneNumber: mobile
    });
  },
  goto: function(dataItem) {
    var t = this;
    wx.showLoading({
      title: '操作中',
    })
    getApp().core.getSetting({
        success: function(e) {
            e.authSetting["scope.userLocation"] ? t.location(dataItem) : getApp().getauth({
                content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                cancel: !1,
                author: "scope.userLocation",
                success: function(e) {
                    e.authSetting["scope.userLocation"] && t.location(dataItem);
                }
            });
        },
        complete: function () {
          wx.hideLoading()
        }
    });
  },
  location: function(e) {
    var item = e.currentTarget.dataset.item;
    getApp().core.openLocation({
        latitude: parseFloat(item.latitude),
        longitude: parseFloat(item.longitude),
        name: item.name,
        address: item.address
    });
  },
  onChoiceShop: function (e) {
    var index = e.currentTarget.dataset.index;
    var shop = this.data.shop_list[index];
    getApp().core.setStorageSync(getApp().const.ADDRESS_INFO, {
      name: shop.name,
      address: shop.address,
      mobile: shop.mobile,
      pic_url: shop.pic_url,
      shop_id: shop.id,
      longitude: shop.longitude,
      latitude: shop.latitude
    })
    wx.switchTab({
      url: "/pages/index/index"
    })
  },
  loadData: function () {
    var e = this;
    wx.showLoading({
      title: '加载中',
    });
    getApp().request({
      url: getApp().api.default.shop_list,
      method: "GET",
      data: {
        page: e.data.page,
        keyword: e.data.keyword,
        longitude: e.data.longitude,
        latitude: e.data.latitude
      },
      success: function (t) {
        if (0 == t.code) {
          if (e.data.page == 0) {
            e.setData({
              shop_list: []
            })
          }
          var list = e.data.shop_list.concat(t.data.list);
          e.setData({
            shop_list: list,
            page_count: t.data.page_count,
            row_count: t.data.row_count,
            page: e.data.page + 1
          });
        } else {
          wx.showToast({
            title: '获取数据失败',
          })
        }
      },
      complete: function () {
        wx.hideLoading()
      }
    });
  },
  goToStore: function () {
    wx.navigateTo({
      url: '/pages/store/store',
    })
  },
  onReachBottom: function () {
    var t = this;
    t.data.page >= t.data.page_count || t.loadData();
  },
  onPullDownRefresh: function () {
    var t = this;
    this.setData({
      keyword: '',
      page: 0
    })
    wx.getLocation({
      success: function (a) {
        t.setData({
          longitude: a.longitude,
          latitude: a.latitude
        });
      },
      complete: function () {
        t.loadData(), wx.stopPullDownRefresh();
      }
    });
  },
  locationContent: function () {
    var o = this;
    getApp().request({
      url: getApp().api.default.get_location,
      method: "GET",
      data: {
        longitude: o.data.longitude,
        latitude: o.data.latitude
      },
      success: function (t) {
        delete t.nation;
        delete t.street_number;
        var arr = [];
        var i = 0;
        for (var item in t) {
          arr[i] = t[item];
          i++;
        }
        t = arr.join('');
        o.setData({
          myAddress: t,
        })
      },
      fail: function (t) {
        o.setData({
          myAddress: "定位失败",
        })
      },
    });
  },
  dingwei: function () {
    var t = this;
    wx.chooseLocation({
      success: function (e) {
        t.setData({
          longitude: e.longitude,
          latitude: e.latitude,
          myAddress: e.address
        })
      },
      fail: function (a) {
        getApp().getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
          success: function (e) {
            e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
              title: "您取消了授权",
              image: "/images/icon-warning.png"
            }));
          }
        });
      },
      complete: function () {
        t.setData({
          page: 0
        })
        t.loadData();
      }
    });
  },
});